#include "stdafx.h"
#include "event~.h"

event:: event(){}	
event::~event(){}	


void event::ontimer(UINT nIDEvent)
{
	if(nIDEvent == 1)
	{
		rand();
		if(ok == 1){loadmap();ok--;}
		if(MCIWndGetPosition(hMCI) >=MCIWndGetLength(hMCI)) 
			MCIWndPlay(hMCI);
		CClientDC dc(hwnd);
		if(t==1)t=0;
		else t++;
		dmap();
		dman();
		dmon();
		dc.BitBlt(0,0,WIDTH,HEIGHT,mdc,0,0,SRCCOPY);
		changemap();				//change map if trigger occur
		if(meetmonster)monster();	//meet monster
	}
	if(nIDEvent == 2)
		textout();
}

void event::keydown()
{
	if(chmap==false && moving==false)
	{
		if(nChar== 0x77 || nChar== 0x57) // up
		{
			d=2;
			if(man.y >0 && maze[man.y-1][man.x]==0)
			{	
				if(man.y-s.y==7 && s.y>0)
					s.y--;
				man.y--;
				ready();
			}
		}
		if(nChar== 0x73 || nChar== 0x53) //down
		{
			d=0;
			if(man.y <mh-1 && maze[man.y+1][man.x]==0)
			{
				if(s.y+14-man.y==7 && s.y+14<mh-1)
					s.y++;
				man.y++;
				ready();
			}
		}
		if(nChar== 0x61 || nChar== 0x41) // left
		{
			d=1;
			if(man.x >0 && maze[man.y][man.x-1]==0)
			{
				if(man.x-s.x==7 && s.x>0)
					s.x--;
				man.x--;
				ready();
			}
		}
		if(nChar== 0x64 || nChar== 0x44) //right
		{
			d=3;
			if(man.x <mw-1 && maze[man.y][man.x+1]==0)
			{
				if(s.x+15-man.x==7 && s.x+15<mw-1)
					s.x++;
				man.x++;
				ready();
			}
		}
	}
}
void event::ready()
{
	if(n==2)
	{
		count++;
		if(count>=5 && rand()%10>=9){meetmonster++;return;}
		if(count>=7 && rand()%8>=7){meetmonster++;return;}
		if(count>=9 && rand()%6>=5){meetmonster++;return;}
		if(count>=12 && rand()%5>=4){meetmonster++;return;}
		if(count>=15 && rand()%4>=3){meetmonster++;return;}
	}
}
void event::textout()
{
	if(tout)
	{
		CClientDC dc(hwnd);
		dc.SelectObject(&font);
		dc.SetBkMode(1);
		dc.SetTextColor(RGB(255,255,255));
		dc.TextOut(200,50,ch,ii);
		if(ii<=len-1)ii++;
		else tout=false;
	}
}